/**
 * Create in 2012-10-12 下午10:23:17.
 */
package kit4j.app.notepad.config;

import java.awt.Font;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import kit4j.app.notepad.annotation.Config;
import kit4j.util.lang.StringUtil;

import com.google.inject.Inject;

/**
 * 
 * @author MiniKnife
 * 
 */
public class NotepadConfig {

	private Properties prop;

	@Inject
	public NotepadConfig(@Config("ConfigFilename") String propertiesFilename) throws ConfigureFaildedException {
		prop = new Properties();
		InputStream is = ClassLoader.getSystemResourceAsStream(propertiesFilename);

		if (is != null) {
			try {
				prop.load(is);
			} catch (IOException e) {
				throw new ConfigureFaildedException(e);
			}
		} else {
			throw new ConfigureFaildedException("未找到指定的配置文件:" + propertiesFilename);
		}
	}

	public String getString(String key) {
		return prop.getProperty(key);
	}

	public String getString(String key, String defaultValue) {
		return prop.getProperty(key, defaultValue);
	}

	public Integer getInteger(String key) {
		String str = getString(key);
		Integer intValue = null;

		try {
			intValue = Integer.valueOf(str);
		} catch (Exception e) {
			intValue = null;
		}

		return intValue;
	}

	public URL getURL(String key) {
		String str = getString(key);

		if (StringUtil.isNotEmpty(str)) {
			URL url = ClassLoader.getSystemResource(str);

			return url;
		} else {
			return null;
		}
	}

	public Image getImage(String key) {
		URL url = getURL(key);

		if (url != null) {
			return new ImageIcon(url).getImage();
		} else {
			return null;
		}
	}

	public Icon getIcon(String key) {
		URL url = getURL(key);

		if (url != null) {
			return new ImageIcon(url);
		} else {
			return null;
		}
	}

	public Icon getIcon(String key, int width, int height) {
		URL url = getURL(key);

		if (url != null) {
			ImageIcon icon = new ImageIcon(url);

			if (icon.getIconWidth() != width || icon.getIconHeight() != height) {
				Image img = icon.getImage();
				img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);

				return new ImageIcon(img);
			} else {
				return icon;
			}
		} else {
			return null;
		}
	}

	public Font getFont(String key) {
		String str = getString(key);

		if (StringUtil.isNotEmpty(str)) {
			String[] fontProperties = str.split("\\p{Punct}");
			String family = fontProperties[0];
			int style = parseFontStyle(fontProperties.length > 1 ? fontProperties[1] : null);
			int size = parseFontSize(fontProperties.length > 2 ? fontProperties[2] : null);

			return new Font(family, style, size);
		} else {
			return null;
		}
	}

	private int parseFontStyle(String styleName) {
		int style = Font.PLAIN;

		if (StringUtil.isNotBlank(styleName)) {
			try {
				style = Integer.parseInt(styleName);
			} catch (NumberFormatException e) {
				if ("plain".equalsIgnoreCase(styleName)) {
					style = Font.PLAIN;
				} else if ("bold".equalsIgnoreCase(styleName)) {
					style = Font.BOLD;
				} else if ("italic".equalsIgnoreCase(styleName)) {
					style = Font.ITALIC;
				} else if ("bold+italic".equalsIgnoreCase(styleName)) {
					style = Font.BOLD + Font.ITALIC;
				}
			}
		}

		return style;
	}

	private int parseFontSize(String sizeName) {
		if (StringUtil.isNotBlank(sizeName)) {
			try {
				return Integer.parseInt(sizeName);
			} catch (NumberFormatException e) {
			}
		}
		return 12;
	}
}
